/**
  ******************************************************************************
  * File Name          : RTC.c
  * Description        : This file provides code for the configuration
  *                      of the RTC instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "rtc.h"

/* USER CODE BEGIN 0 */
DateTime datetime;

void RTC_UPDATE(void)
{
	HAL_RTC_GetDate(&hrtc, &(datetime.date), RTC_FORMAT_BIN);
	HAL_RTC_GetTime(&hrtc, &(datetime.time), RTC_FORMAT_BIN);
	
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, datetime.time.Seconds);
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, datetime.time.Minutes);
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3, datetime.time.Hours);
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4, datetime.date.Date);
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, datetime.date.Month);
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR6, datetime.date.Year);
}

void RTC_SET(void)
{
	datetime.time.Seconds = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);
	datetime.time.Minutes = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
	datetime.time.Hours =   HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3);
	datetime.date.Date =  HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4);
	datetime.date.Month = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
	datetime.date.Year =  HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR6);
	
	HAL_RTC_SetTime(&hrtc, &datetime.time, RTC_FORMAT_BIN);
	HAL_RTC_SetDate(&hrtc, &datetime.date, RTC_FORMAT_BIN);
}

/* USER CODE END 0 */

RTC_HandleTypeDef hrtc;

/* RTC init function */
void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef DateToUpdate = {0};

  /** Initialize RTC Only 
  */
  hrtc.Instance = RTC;
  hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */
    
  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date 
  */
  sTime.Hours = 12;
  sTime.Minutes = 30;
  sTime.Seconds = 30;

  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
  DateToUpdate.Month = RTC_MONTH_JANUARY;
  DateToUpdate.Date = 1;
  DateToUpdate.Year = 19;

  if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{

  if(rtcHandle->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */

  /* USER CODE END RTC_MspInit 0 */
    HAL_PWR_EnableBkUpAccess();
    /* Enable BKP CLK enable for backup registers */
    __HAL_RCC_BKP_CLK_ENABLE();
    /* RTC clock enable */
    __HAL_RCC_RTC_ENABLE();
  /* USER CODE BEGIN RTC_MspInit 1 */

  /* USER CODE END RTC_MspInit 1 */
  }
}

void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
{

  if(rtcHandle->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspDeInit 0 */

  /* USER CODE END RTC_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_RTC_DISABLE();
  /* USER CODE BEGIN RTC_MspDeInit 1 */

  /* USER CODE END RTC_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
